package com.litong.litongjiaoyu.controller;

import com.litong.litongjiaoyu.annotation.IgnoreAuth;
import com.litong.litongjiaoyu.utils.JSONResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @ClassName PicController
 * @Description 图片控制器
 * @Author jingz
 * @Date 2019/11/27 16:34
 * @Version 1.0
 **/
@RestController
@RequestMapping("/pic")
public class PicController {
    private static final Logger LOGGER = LoggerFactory.getLogger(PicController.class);

    /**
     * 上传图片至目录
     *
     * @param file 图片文件
     * @return 上传状态或图片名称
     */
    @IgnoreAuth
    @ResponseBody
    @RequestMapping("/uploadimage")
    public String getFile (@RequestParam("file") MultipartFile file) {
        LOGGER.info("收到图片上传请求，正在处理");
        if (file.isEmpty()) {
            LOGGER.error("图片上传失败");
            return "上传失败";
        }

        String fileName = file.getOriginalFilename();
        String filePath = "/root/data/img/";
        //String filePath = "E:\\JAVA\\litongjiaoyu\\data\\img";
        File dest = new File(filePath + fileName);

        if (! dest.exists()) {
            try {
                file.transferTo(dest);
                LOGGER.info("临时图片上传成功");
                return fileName;
            } catch (IOException e) {
                LOGGER.error("临时图片上传失败：" + e.getMessage());
                return "fail";
            }
        } else {
            return fileName;
        }
    }

    /**
     * 根据图片名称返回本地图片
     *
     * @param name 图片名称
     * @return 返回本地图片
     */
    @IgnoreAuth
    @ResponseBody
    @GetMapping("/get")
    public byte[] getPicture (String name) {
        LOGGER.info("请求图片, 图片名为：" + name);
        String filePath = "/root/data/img/";
        //String filePath = "E:\\JAVA\\litongjiaoyu\\data\\img\\";
        synchronized (this) {

            File file = new File(filePath + name);
            FileInputStream inputStream = null;
            try {
                inputStream = new FileInputStream(file);
                byte[] bytes = new byte[inputStream.available()];
                inputStream.read(bytes, 0, inputStream.available());
                LOGGER.info("返回成功");
                return bytes;
            } catch (FileNotFoundException e) {
                LOGGER.error("返回本地图片失败：" + e.getMessage());
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
                LOGGER.error("返回本地图片失败：" + e.getMessage());
            } finally {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    LOGGER.error("文件流关闭失败：" + e.getMessage());
                }
            }
            return null;
        }
    }

    /**
     * 根据图片名称删除图片
     *
     * @param name 图片名称
     * @return json
     */

    @PostMapping("/del")
    public JSONResult deletePicture (String name) {
        LOGGER.info("开始删除照片：" + name);

//        String filePath = "/root/data/img/";
        String filePath = "E:\\JAVA\\litongjiaoyu\\data\\img\\";
        File file = new File(filePath + name);

        if (file.exists()) {
            if (file.delete()) {
                LOGGER.info("图片删除成功");
                return JSONResult.ok("true");
            } else {
                LOGGER.error("图片删除失败");
                return JSONResult.errorMsg("图片删除失败");
            }
        } else {
            LOGGER.warn("图片不存在：" + name);
            return JSONResult.errorException("图片不存在");
        }
    }
}
